#include "algorithm.h"
#include <iostream>

using namespace std;
Algorithm::Algorithm()
{

}

void Algorithm::generateRandom()  //产生随机数
{
    int zeroNumber = 0;//元素中为零的个数
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
            if(board[i][j] == 0)
                zeroNumber++;
    }

    if(zeroNumber != 0 && judgeMove == 1)
    {
        judgeMove = 0;
        //srand((unsigned)time(nullptr));
        int newNumber = rand()%10 < 9 ? 2 : 4;   //新随机数的值
        int changePlace = rand()%zeroNumber;  //第changePlace个为0元素将会被替代
        for(int i = 0; i < 4; i++)
        {
            for(int j = 0; j < 4; j++)
            {
                if(board[i][j] == 0)
                {
                    if(changePlace == 0)
                    {
                        board[i][j] = newNumber;
                        changePlace--;

                        break;
                    }
                    else
                        changePlace--;
                }
            }
            if(changePlace < 0)
            {
                break;
            }
        }
    }
}

int Algorithm::sameElment()   //判断是否有相邻的相等项或=0的项
{
    for (int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            if(board[i][j]==0)
            {
                return 1;
            }
        }
    }
    for (int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(board[i][j]==board[i][j+1] || board[i][j] == board[i+1][j])
                return 1;
        }
        if(board[i][3] == board[i+1][3])
            return 1;
    }

    return 0;
}


void Algorithm::changMatrixUp()  //Up修改矩阵元素
{

    for(int i = 0; i < 4; i++)
    {
        for(int k = 0; k < 3; k++)
            for(int j = 3; j > 0; j--)
            {
                if(board[j-1][i] == 0)
                {
                   board[j-1][i] = board[j][i];
                    board[j][i] = 0;
                    if(board[j-1][i] != 0)
                        judgeMove = 1;
                }
            }

    }

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(board[j][i] == board[j+1][i])
            {
                board[j][i] = board[j][i]*2;
                score=score+board[j][i];
                if(board[j][i] != board[j+1][i])
                    judgeMove = 1;
                switch(j)
                {
                    case 0:
                    {
                        board[j+1][i] = board[j+2][i];
                        board[j+2][i] = board[j+3][i];
                        board[j+3][i] = 0;
                        break;
                    }
                    case 1:
                    {
                        board[j+1][i] = board[j+2][i];
                        board[j+2][i] = 0;
                        break;
                    }
                    case 2:
                    {
                        board[j+1][i] = 0;
                        break;
                    }
                }

            }
        }
    }


}

void Algorithm::changMatrixDown()  //Down修改矩阵元素
{
    for(int i = 0; i < 4; i++)
    {
        for(int k = 0; k < 3; k++)
            for(int j = 0; j < 3; j++)
            {
                if(board[j+1][i] == 0)
                {
                    board[j+1][i] = board[j][i];
                    if(board[j+1][i] != 0)
                        judgeMove = 1;
                    board[j][i] = 0;
                }
            }

    }

    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > 0; j--)
        {
            if(board[j][i] == board[j-1][i])
            {
                board[j][i] = board[j][i]*2;
                score=score+board[j][i];
                if(board[j][i] != board[j-1][i])
                    judgeMove = 1;
                switch(j)
                {
                    case 3:
                    {
                        board[j-1][i] = board[j-2][i];
                        board[j-2][i] = board[j-3][i];
                        board[j-3][i] = 0;
                        break;
                    }
                    case 2:
                    {
                        board[j-1][i] = board[j-2][i];
                        board[j-2][i] = 0;
                        break;
                    }
                    case 1:
                    {
                        board[j-1][i] = 0;
                        break;
                    }
                }
            }
        }

    }

}

void Algorithm::changMatrixLeft()  //Left修改矩阵元素
{
    int intermediate;
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = board[i][j];
            board[i][j] = board[j][i];
            board[j][i] = intermediate;
        }

    }
    changMatrixUp();
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = board[i][j];
            board[i][j] = board[j][i];
            board[j][i] = intermediate;
        }

    }

}

void Algorithm::changMatrixRight()  //Right修改矩阵元素
{
    int intermediate;
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = board[i][j];
            board[i][j] = board[j][i];
            board[j][i] = intermediate;
        }

    }
    changMatrixDown();
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = board[i][j];
            board[i][j] = board[j][i];
            board[j][i] = intermediate;
        }

    }

}

int Algorithm::getScore()
{
    return score;
}

void Algorithm::clearScore()
{
    score=0;
}
